home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2004 April / Gamestar_61_2004-04_dvdb.iso / DVDStar / Editace / hltp.exe / {app} / Source Code / Textures SDK / qlumpy / qlumpy.c next >
C/C++ Source or Header  |  1998-12-08  |  9KB  |  430 lines

  1. /***
  2. *
  3. *    Copyright (c) 1998, Valve LLC. All rights reserved.
  4. *    
  5. *    This product contains software technology licensed from Id 
  6. *    Software, Inc. ("Id Technology").  Id Technology (c) 1996 Id Software, Inc. 
  7. *    All Rights Reserved.
  8. *
  9. ****/
  10.  
  11. #define VERSION "2.2"
  12. #include "qlumpy.h"
  13.  
  14.  
  15. #define MAXLUMP        0x50000         // biggest possible lump
  16.  
  17. extern char qproject[];
  18.  
  19. int                grabbed;
  20.  
  21. byte            *byteimage, *lbmpalette;
  22. int              byteimagewidth, byteimageheight;
  23.  
  24. char            basepath[1024];
  25. char            lumpname[16];
  26.  
  27. char            destfile[1024];
  28.  
  29. byte            *lumpbuffer, *lump_p;
  30.  
  31. qboolean        savesingle;
  32. qboolean        outputcreated;
  33.  
  34. qboolean        do16bit;
  35. qboolean        fTransparent255;
  36.  
  37. /*
  38. =============================================================================
  39.  
  40.                             MAIN
  41.  
  42. =============================================================================
  43. */
  44.  
  45. void GrabRaw (void);
  46. void GrabPalette (void);
  47. void GrabPic (void);
  48. void GrabMip (void);
  49. void GrabColormap (void);
  50. void GrabColormap2 (void);
  51. void GrabFont( void );
  52.  
  53. typedef struct
  54. {
  55.     char    *name;
  56.     void    (*function) (void);
  57. } command_t;
  58.  
  59. command_t       commands[] =
  60. {
  61.     { "palette", GrabPalette },
  62.     { "colormap", GrabColormap },
  63.     { "qpic", GrabPic },
  64.     { "miptex", GrabMip },
  65.     { "raw", GrabRaw },
  66.  
  67.     { "colormap2", GrabColormap2 },
  68.     { "font", GrabFont },
  69.  
  70.     { NULL, NULL }                     // list terminator
  71. };
  72.  
  73.  
  74. #define TRANSPARENT_R        0x0
  75. #define TRANSPARENT_G        0x0
  76. #define TRANSPARENT_B        0xFF
  77. #define IS_TRANSPARENT(p)    (p[0]==TRANSPARENT_R && p[1]==TRANSPARENT_G && p[2]==TRANSPARENT_B)
  78. /*
  79. ==============
  80. TransparentByteImage
  81. ==============
  82. */
  83. void TransparentByteImage( void )
  84. {
  85.     // Remap all pixels of color 0,0,255 to index 255 and remap index 255 to something else
  86.     byte    transtable[256], *image;
  87.     int        i, j, firsttrans;
  88.  
  89.     firsttrans = -1;
  90.     for ( i = 0; i < 256; i++ ) {
  91.         if ( IS_TRANSPARENT( (lbmpalette+(i*3)) ) ) {
  92.             transtable[i] = 255;
  93.             if ( firsttrans < 0 )
  94.                 firsttrans = i;
  95.         }
  96.         else
  97.             transtable[i] = i;
  98.     }
  99.  
  100.     // If there is some transparency, translate it
  101.     if ( firsttrans >= 0 ) {
  102.         if ( !IS_TRANSPARENT( (lbmpalette+(255*3)) ) )
  103.             transtable[255] = firsttrans;
  104.         image = byteimage;
  105.         for ( j = 0; j < byteimageheight; j++ ) {
  106.             for ( i = 0; i < byteimagewidth; i++ ) {
  107.                 *image = transtable[*image];
  108.                 image++;
  109.             }
  110.         }
  111.         // Move palette entry for pixels previously mapped to entry 255
  112.         lbmpalette[ firsttrans*3 + 0 ] = lbmpalette[ 255*3 + 0 ];
  113.         lbmpalette[ firsttrans*3 + 1 ] = lbmpalette[ 255*3 + 1 ];
  114.         lbmpalette[ firsttrans*3 + 2 ] = lbmpalette[ 255*3 + 2 ];
  115.         lbmpalette[ 255*3 + 0 ] = TRANSPARENT_R;
  116.         lbmpalette[ 255*3 + 1 ] = TRANSPARENT_G;
  117.         lbmpalette[ 255*3 + 2 ] = TRANSPARENT_B;
  118.     }
  119. }
  120.  
  121.  
  122.  
  123. /*
  124. ==============
  125. LoadScreen
  126. ==============
  127. */
  128. void LoadScreen (char *name)
  129. {
  130.     char    *expanded;
  131.  
  132.     expanded = ExpandPathAndArchive (name);
  133.  
  134.     printf ("grabbing from %s...\n",expanded);
  135.     LoadLBM (expanded, &byteimage, &lbmpalette);
  136.  
  137.     byteimagewidth = bmhd.w;
  138.     byteimageheight = bmhd.h;
  139. }
  140.  
  141.  
  142. /*
  143. ==============
  144. LoadScreenBMP
  145. ==============
  146. */
  147. void LoadScreenBMP(char *pszName)
  148. {
  149.     char    *pszExpanded;
  150.     char    basename[64];
  151.     
  152.     pszExpanded = ExpandPathAndArchive(pszName);
  153.  
  154.     printf("grabbing from %s...\n", pszExpanded);
  155.     if (LoadBMP(pszExpanded, &byteimage, &lbmpalette))
  156.         Error ("Failed to load!", pszExpanded);
  157.  
  158.     if ( byteimage == NULL || lbmpalette == NULL )
  159.         Error("FAIL!",pszExpanded);
  160.     byteimagewidth = bmhd.w;
  161.     byteimageheight = bmhd.h;
  162.  
  163.     ExtractFileBase (token, basename);        // Files that start with '$' have color (0,0,255) transparent,
  164.     if ( basename[0] == '{' ) {                // move to last palette entry.
  165.         fTransparent255 = true;
  166.         TransparentByteImage();
  167.     }
  168. }
  169.  
  170.  
  171. /*
  172. ================
  173. CreateOutput
  174. ================
  175. */
  176. void CreateOutput (void)
  177. {
  178.     outputcreated = true;
  179. //
  180. // create the output wadfile file
  181. //
  182.     NewWad (destfile, false);    // create a new wadfile
  183. }
  184.  
  185. /*
  186. ===============
  187. WriteLump
  188. ===============
  189. */
  190. void WriteLump (int type, int compression)
  191. {
  192.     int        size;
  193.     
  194.     if (!outputcreated)
  195.         CreateOutput ();
  196.  
  197. //
  198. // dword align the size
  199. //
  200.     while ((int)lump_p&3)
  201.         *lump_p++ = 0;
  202.  
  203.     size = lump_p - lumpbuffer;
  204.     if (size > MAXLUMP)
  205.         Error ("Lump size exceeded %d, memory corrupted!",MAXLUMP);
  206.  
  207. //
  208. // write the grabbed lump to the wadfile
  209. //
  210.     AddLump (lumpname,lumpbuffer,size,type, compression);
  211. }
  212.  
  213. /*
  214. ===========
  215. WriteFile
  216.  
  217. Save as a seperate file instead of as a wadfile lump
  218. ===========
  219. */
  220. void WriteFile (void)
  221. {
  222.     char    filename[1024];
  223.     char    *exp;
  224.  
  225.     sprintf (filename,"%s/%s.lmp", destfile, lumpname);
  226.     exp = ExpandPath(filename);
  227.     printf ("saved %s\n", exp);
  228.     SaveFile (exp, lumpbuffer, lump_p-lumpbuffer);        
  229. }
  230.  
  231. /*
  232. ================
  233. ParseScript
  234. ================
  235. */
  236. void ParseScript (void)
  237. {
  238.     int            cmd;
  239.     int            size;
  240.  
  241.     fTransparent255 = false;
  242.     do
  243.     {
  244.         //
  245.         // get a command / lump name
  246.         //
  247.         GetToken (true);
  248.         if (endofscript)
  249.             break;
  250.         if (!Q_strcasecmp (token,"$LOAD"))
  251.         {
  252.             GetToken (false);
  253.             LoadScreen (token);
  254.             continue;
  255.         }
  256.  
  257.         if (!Q_strcasecmp (token,"$DEST"))
  258.         {
  259.             GetToken (false);
  260.             strcpy (destfile, token);
  261.             continue;
  262.         }
  263.  
  264.         if (!Q_strcasecmp (token,"$SINGLEDEST"))
  265.         {
  266.             GetToken (false);
  267.             strcpy (destfile, token);
  268.             savesingle = true;
  269.             continue;
  270.         }
  271.  
  272.  
  273.         if (!Q_strcasecmp (token,"$LOADBMP"))
  274.         {
  275.             GetToken (false);
  276.             fTransparent255 = false;
  277.             LoadScreenBMP (token);
  278.             continue;
  279.         }
  280.  
  281.         //
  282.         // new lump
  283.         //
  284.         if (strlen(token) >= sizeof(lumpname) )
  285.             Error ("\"%s\" is too long to be a lump name",token);
  286.         memset (lumpname,0,sizeof(lumpname));            
  287.         strcpy (lumpname, token);
  288.         for (size=0 ; size<sizeof(lumpname) ; size++)
  289.             lumpname[size] = tolower(lumpname[size]);
  290.  
  291.         //
  292.         // get the grab command
  293.         //
  294.         lump_p = lumpbuffer;
  295.  
  296.         GetToken (false);
  297.  
  298.         //
  299.         // call a routine to grab some data and put it in lumpbuffer
  300.         // with lump_p pointing after the last byte to be saved
  301.         //
  302.         for (cmd=0 ; commands[cmd].name ; cmd++)
  303.             if ( !Q_strcasecmp(token,commands[cmd].name) )
  304.             {
  305.                 commands[cmd].function ();
  306.                 break;
  307.             }
  308.  
  309.         if ( !commands[cmd].name )
  310.             Error ("Unrecognized token '%s' at line %i",token,scriptline);
  311.     
  312.         grabbed++;
  313.         
  314.         if (savesingle)
  315.             WriteFile ();
  316.         else    
  317.             WriteLump (TYP_LUMPY+cmd, 0);
  318.         
  319.     } while (!endofscript);
  320. }
  321.  
  322. /*
  323. =================
  324. ProcessLumpyScript
  325.  
  326. Loads a script file, then grabs everything from it
  327. =================
  328. */
  329. void ProcessLumpyScript (char *basename)
  330. {
  331.     char            script[256];
  332.  
  333.     printf ("qlumpy script: %s\n",basename);
  334.     
  335. //
  336. // create default destination directory
  337. //
  338.     strcpy (destfile, ExpandPath(basename));
  339.     StripExtension (destfile);
  340.     strcat (destfile,".wad");        // unless the script overrides, save in cwd
  341.  
  342. //
  343. // save in a wadfile by default
  344. //
  345.     savesingle = false;
  346.     grabbed = 0;
  347.     outputcreated = false;
  348.     
  349.     
  350. //
  351. // read in the script file
  352. //
  353.     strcpy (script, basename);
  354.     DefaultExtension (script, ".ls");
  355.     LoadScriptFile (script);
  356.     
  357.     strcpy (basepath, basename);
  358.     
  359.     ParseScript ();                // execute load / grab commands
  360.     
  361.     if (!savesingle)
  362.     {
  363.         WriteWad (do16bit);                // write out the wad directory
  364.         printf ("%i lumps grabbed in a wad file\n",grabbed);
  365.     }
  366.     else
  367.         printf ("%i lumps written seperately\n",grabbed);
  368. }
  369.  
  370.  
  371. /*
  372. ==============================
  373. main
  374. ==============================
  375. */
  376. int main (int argc, char **argv)
  377. {
  378.     int        i;
  379.     
  380.     printf ("\nqlumpy "VERSION" by John Carmack, copyright (c) 1994 Id Software.\n");
  381.     printf ("Portions copyright (c) 1998 Valve LLC (%s)\n", __DATE__ );
  382.  
  383.     if (argc == 1)
  384.         Error ("qlumpy [-archive directory] [-8bit] [-proj <project>] scriptfile [scriptfile ...]");
  385.  
  386.     lumpbuffer = malloc (MAXLUMP);
  387.     do16bit = true;
  388.  
  389.     for( i=1; i<argc; i++ )
  390.     {
  391.         if( *argv[ i ] == '-' )
  392.         {
  393.             if( !strcmp( argv[ i ], "-archive" ) )
  394.             {
  395.                 archive = true;
  396.                 strcpy (archivedir, argv[2]);
  397.                 printf ("Archiving source to: %s\n", archivedir);
  398.             }
  399.             else if( !strcmp( argv[ i ], "-proj" ) )
  400.             {
  401.                 strcpy( qproject, argv[ i + 1 ] );
  402.                 i++;
  403.             }
  404.             else if( !strcmp( argv[ i ], "-8bit" ) )
  405.                 do16bit = false;
  406.         }
  407.         else
  408.             break;
  409.     }
  410.  
  411.     // rest of arguments are script files
  412.     for ( ; i<argc ; i++)
  413.     {
  414.         char szTemp[1024];
  415.         char *pszPath = argv[i];
  416.  
  417.         // Fully qualify the path names before using them
  418.  
  419.         if (!(pszPath[0] == '/' || pszPath[0] == '\\' || pszPath[1] == ':'))
  420.         {    // path is partial
  421.             Q_getwd (szTemp);
  422.             strcat (szTemp, pszPath);
  423.             pszPath = szTemp;
  424.         }
  425.         SetQdirFromPath(pszPath);
  426.         ProcessLumpyScript(pszPath);
  427.     }
  428.         
  429.     return 0;
  430. }